import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import databaseConfig from './config/database.config';
import authConfig from './config/auth.config';
import { AdminUsersModule } from './admin-users/admin-users.module';
import { RoleModule } from './role/role.module';
import { AuthModule } from './auth/auth.module';
import { CategoryModule } from './category/category.module';
import appConfig from './config/app.config';
import { TypeOrmConfigService } from './database/typeorm-config.service';
import { DataSource } from 'typeorm';
import { PermissionModule } from './permission/permission.module';
import { MenuModule } from './menu/menu.module';
import { RolePermissionModule } from './role-permission/role-permission.module';
import { AuthPermissionGuard } from './permission/permission.gurad';
import { APP_GUARD } from '@nestjs/core';

@Module({
	imports: [
		ConfigModule.forRoot({
			isGlobal: true,
			load: [databaseConfig, authConfig, appConfig],
			envFilePath: ['.env'],
		}),
		TypeOrmModule.forRootAsync({
			useClass: TypeOrmConfigService,
			dataSourceFactory: async (options) => {
				const dataSource = await new DataSource(options).initialize();

				return dataSource;
			},
		}),
		AdminUsersModule,
		RoleModule,
		CategoryModule,
		AuthModule,
		PermissionModule,
		MenuModule,
		RolePermissionModule,
	],
	providers: [{ provide: APP_GUARD, useClass: AuthPermissionGuard }],
})
export class AppModule {}
